<section id="recommend">
    <div class="row">
        <div class="container">
            <div class="col-lg-12 column12">
                <div id="bread">
                    <ol class="breadcrumb">
                        <li><a href="<?php echo SITE_URL; ?>">Home</a></li>
                        <li><?php
                            if ($classcoursetype == "classes") {
                                echo CHtml::link($classDetails->class_titile . ' ', $this->createAbsoluteUrl('workshop-'.$classDetails->id.'/'.$classDetails->class_seo_title));
                            } else {
                                echo CHtml::link($classDetails->course_titile . ' ', $this->createAbsoluteUrl('courses-'.$classDetails->id.'/'.$classDetails->course_seo_title));
                            }
                            ?></li>
                        <li class="active">recommend a friend</li>
                    </ol>
                </div><!-- breadcrumb div ends here -->
            </div>
            <div class="col-lg-12 column12">
                <h1><?php echo ($classcoursetype == "classes") ? $classDetails->class_titile : $classDetails->course_titile; ?></h1>

                <div class="course-detail">
                    <div id="formResult" style="color:#747474"></div>
                    <?php
                    $form = $this->beginWidget('CActiveForm', array(
                        'id' => 'recommend-to-user-form',
                        'enableAjaxValidation' => false,
                        'htmlOptions' => array(
                            'onsubmit' => "return false;", /* Disable normal form submit */
                        ),
                    ));
                    ?>
                    <small>Enter your detail below</small>
                    <div class="row detail">
                        <div class="col-lg-4 col-md-4 col-sm-6 pad-left column4">
                            <div class="col-lg-4 col-md-4 col-sm-4 pad-left column4"> 
                                <?php echo $form->labelEx($recommendFromUser, 'from_name'); ?>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 pad-left column8">  
                                <input name="classcoursetype" type="hidden" value="<?php echo $classcoursetype; ?>">
                                <?php if ($classcoursetype == "classes") { ?>
                                    <?php echo $form->hiddenField($recommendFromUser, 'classes_id', array('value' => $classDetails->id)); ?>
                                <?php } else { ?>
                                    <?php echo $form->hiddenField($recommendFromUser, 'courses_id', array('value' => $classDetails->id)); ?>
                                <?php } ?>
                                <?php echo $form->textField($recommendFromUser, 'from_name', array('maxlength' => 45, 'class' => 'form-control requiredfield', 'id' => 'from_name_1', 'value' => ($profileDetails != '') ? $profileDetails->first_name : '')); ?>
                                <div class="errorMessage" style="display: none;" id="error_from_name_1">Name cannot be blank.</div>

                            </div>
                        </div>   

                        <div class="col-lg-4 col-md-4 col-sm-6 pad-left column4">
                            <div class="col-lg-4 col-md-4 col-sm-4 pad-left column4"> 
                                <?php echo $form->labelEx($recommendFromUser, 'from_email'); ?>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 pad-left column8">                                 
                                <?php echo $form->textField($recommendFromUser, 'from_email', array('maxlength' => 45, 'class' => 'form-control requiredfield email', 'id' => 'from_email_1', 'value' => ($profileDetails != '') ? $profileDetails->user->username : '')); ?>
                                <div class="errorMessage" style="display: none;" id="error_email_1">Email cannot be blank.</div>
                                <div class="errorMessage" id="error_email_1"></div>
                            </div>
                        </div> 

                        <div class="col-lg-4 col-md-4 col-sm-6 pad-left column4">
                            <div class="col-lg-4 col-md-4 col-sm-4 pad-left column4"> 
                                <?php echo $form->labelEx($recommendFromUser, 'from_phone_no'); ?>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 pad-left column8"> 
                                <?php echo $form->textField($recommendFromUser, 'from_phone_no', array('maxlength' => 12, 'class' => 'form-control ', 'value' => ($profileDetails != '') ? $profileDetails->phone : '')); ?>
                                <?php echo $form->error($recommendFromUser, 'from_phone_no'); ?>
                            </div>
                        </div>

                    </div>
                    <hr>   
                    <!-- friends detail row starts here -->
                    <small>Enter your friend's detail below</small>
                    <div class="row detail" >
                          <div  id="addtofriend">
                        <div class="col-lg-4 col-md-4 col-sm-6 pad-left column4">
                            <div class="col-lg-4 col-md-4 col-sm-4 pad-left column4"> 
                                <label><?php echo $form->labelEx($model, 'to_name'); ?></label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 pad-left column8">  
                                <?php echo $form->textField($model, 'to_name[]', array('maxlength' => 45, 'class' => 'form-control requiredfield', 'id' => 'to_name_0')); ?>
                                <div class="errorMessage" style="display: none;" id="error_to_name_0">Name cannot be blank.</div>
                            </div>
                        </div>   

                        <div class="col-lg-4 col-md-4 col-sm-6 pad-left column4">
                            <div class="col-lg-4 col-md-4 col-sm-4 pad-left column4"> 
                                <label><?php echo $form->labelEx($model, 'to_email'); ?></label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 pad-left column8">  
                                <?php echo $form->textField($model, 'to_email[]', array('maxlength' => 45, 'class' => 'form-control requiredfield email', 'id' => 'to_email_0')); ?>
                                <div class="errorMessage" style="display: none;" id="error_to_email_0">Email cannot be blank.</div>
                                <div class="errorMessage"  id="error_to_email_0"></div>
                            </div>
                        </div> 

                        <div class="col-lg-4 col-md-4 col-sm-6 pad-left column4">
                            <div class="col-lg-4 col-md-4 col-sm-4 pad-left column4"> 
                                <label><?php echo $form->labelEx($model, 'to_phone_no'); ?></label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 pad-left column8">  
                                <?php echo $form->textField($model, 'to_phone_no[]', array('maxlength' => 12, 'class' => 'form-control')); ?>
                                <?php echo $form->error($model, 'to_phone_no'); ?>
                            </div>
                        </div> <div style="clear:both"></div>
                          </div>
                        
                        <a href="javascript:void(0)" onclick="addMoreFrienddetails();">+Add More</a>
                    </div>
                    <hr>
                    <!-- message for your friend -->
                    <div class="row leave-msg">
                        <small>Write a message for your friend</small>
                        <div class="gap"></div>
                        <?php echo $form->textArea($model, 'to_message_text', array('class' => 'form-control')); ?>
                         <?php echo GxHtml::submitButton(Yii::t('app', 'Recommend'), array('id' => 'recommend_to_frd')); ?>
                       <span id="recomendedLoder" style="display: none"><img class="recomendloader"src="<?php echo ThemePath(); ?>/images/myskill.gif"></img></span>
                          
                    </div>
                    <?php $this->endWidget(); ?>   
                </div>
            </div>
        </div>
    </div>
</section>


<script>
    var counter = 1;
    function addMoreFrienddetails() {
        counter = counter + 1;
        $("#addtofriend").append('<div class="addtofriends' + counter + '" id="remove_addtofriends' + counter + '"> <div class="col-lg-4 col-md-4 col-sm-6 pad-left column4">\n\
 <div class="col-lg-4 col-md-4 col-sm-4 pad-left column4"> <label><label class="required" for="RecommendToUser_to_name">Name <span class="required">*</span></label></label>\n\
</div><div class="col-lg-8 col-md-8 col-sm-8 pad-left column8"><input type="text" name="RecommendToUser[to_name][]" id="to_name_' + counter + '" class="form-control requiredfield" maxlength="45">\n\
<div id="error_to_name_' + counter + '" style="display: none;" class="errorMessage">Name cannot be blank.</div></div></div>\n\
<div class="col-lg-4 col-md-4 col-sm-6 pad-left column4"><div class="col-lg-4 col-md-4 col-sm-4 pad-left column4">\n\
<label><label class="required" for="RecommendToUser_to_email">Email <span class="required">*</span></label></label></div>\n\
<div class="col-lg-8 col-md-8 col-sm-8 pad-left column8"><input type="text" name="RecommendToUser[to_email][]" id="to_email_' + counter + '" class="form-control requiredfield email" maxlength="45">\n\
<div id="error_to_email_' + counter + '" style="display: none;" class="errorMessage">Email cannot be blank.</div>\n\
<div id="error_to_email_' + counter + '" class="errorMessage"></div></div></div><div class="col-lg-4 col-md-4 col-sm-6 pad-left column4">\n\
<div class="col-lg-4 col-md-4 col-sm-4 pad-left column4"> <label><label for="RecommendToUser_to_phone_no">Phone No</label></label>\n\
</div><div class="col-lg-8 col-md-8 col-sm-8 pad-left column8"><input type="text" id="RecommendToUser_to_phone_no" name="RecommendToUser[to_phone_no][]" class="form-control" maxlength="12">\n\
</div><a href="javascript:void(0)" id="addtofriends' + counter + '" onclick=removeFrienddetails(this.id);>-Remove</div><div style="clear:both"></div>');
 }

function removeFrienddetails(id) {
         $("#remove_" + id).remove();
     }
     $('#recommend_to_frd').click(function(e) {
         var isValid = true;
         $('.requiredfield').each(function() {
             if ($.trim($(this).val()) == '') {
                 var errorid = $('#' + this.id).next('div').attr("id");
                 $("#" + errorid).show();
                 isValid = false;

             }
             else if ($.trim($(this).val()) != '') {
                 var errorid = $('#' + this.id).next('div').attr("id");
                 $("#" + errorid).hide();
                 $("#errorAnchor").hide();
                 $('.email').each(function() {
                     var email = $('#' + this.id).val();
                     var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                     if (!regex.test(email)) {
                         var errorid = $('#' + this.id).next('div').next('div').attr("id");
                         $("#" + errorid).text('');
                         $("#" + errorid).text('This email is not valid.').show();
                         isValid = false;
                     }
                 });

             } else {
                 var errorid = $('#' + this.id).next('div').attr("id");
                 $("#" + errorid).hide();
                 $("#errorAnchor").hide();
             }
         });
         if (isValid == false)
             e.preventDefault();
         if (isValid == true) {
             jQuery.ajax({
                 'type': 'POST',
                 'url': "<?php echo Yii::app()->createAbsoluteUrl("RecommendToUser/SaveRecommendtoFreind"); ?>",
                 'data': $("#recommend-to-user-form").serialize(),
                 'beforeSend': function(jqXHR, settings) {
                     $("#recomendedLoder").show();
                 },
                 'success': function(data) {
                     $("#recomendedLoder").hide();
                     $("#frd_success").show();
                     $("#formResult").html("Your message has been sent successfully.");
                     $("#recommend-to-user-form").hide();
                 },
                 'cache': false
             });
         }


     });
</script>
